package de.lmu.mvs.lerngruppenfinder;

import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import de.lmu.mvs.lerngruppenfinder.R.id;
import de.lmu.mvs.lerngruppenfinder.utils.BaseActivity;
import de.lmu.mvs.lerngruppenfinder.utils.FileApi;
import de.lmu.mvs.lerngruppenfinder.utils.ServerConnector;
import de.lmu.mvs.lerngruppenfinder.utils.ServerConnector.ServerConnectorListener;
import de.lmu.mvs.lerngruppenfinder.utils.ServerConnector.ServerError;


public class CommentsActivity extends BaseActivity {
	private boolean[] preloaderSuccess;
	private final int prelaodAmaount=2;
	private int eventID;
	private TextView commentsTitle;
	private LinearLayout commentsContainer;
	private Button comment;
	
	private static final String TAG = MainActivity.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comments);
		
		commentsTitle = (TextView) findViewById(R.id.comments_title);
		commentsContainer = (LinearLayout) findViewById(R.id.comments_container);

		if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB){
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
		
		
		eventID = getIntent().getIntExtra("id", 0);
		
		ServerConnector.getComments(new ServerConnectorListener<JsonArray>() {

			@Override
			public void requestFinished(JsonArray json) {
				
				populateComments(json);
				
			}

			@Override
			public void requestError(ServerError error) {
				Log.e("Comments", error.getErrorMessage());
			}
		}, eventID);

		comment = (Button) findViewById(R.id.docomment);
		comment.setOnClickListener(new OnClickListener() {
			 
			
			
			@Override
			public void onClick(View arg0) {
 
				// get prompts.xml view
				LayoutInflater li = LayoutInflater.from(CommentsActivity.this);
				View promptsView = li.inflate(R.layout.comment_prompt, null);
 
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CommentsActivity.this);
 
				// set prompts.xml to alertdialog builder
				alertDialogBuilder.setView(promptsView);
				final EditText userInput = (EditText) promptsView
						.findViewById(R.id.editTextDialogUserInput);
 
				// set dialog message
				alertDialogBuilder
					.setCancelable(false)
					.setPositiveButton("OK",
					  new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog,int id) {
						// get user input and set it to result
						// edit text
					    ServerConnector.comment(new ServerConnectorListener<JsonArray>() {

							@Override
							public void requestFinished(JsonArray json) {
								populateComments(json);							
							}

							@Override
							public void requestError(ServerError error) {
								Toast.makeText(getBaseContext(), "Please try to submit later", Toast.LENGTH_SHORT).show();
								// TODO Auto-generated method stub
								
							}
						}, FileApi.readLoginCredentials().id, eventID, userInput.getText().toString(), 0);

					    }
					  })
					.setNegativeButton("Cancel",
					  new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog,int id) {
						dialog.cancel();
					    }
					  });
 
				// create alert dialog
				AlertDialog alertDialog = alertDialogBuilder.create();
 
				// show it
				alertDialog.show();
 
			}
		});
	}
		
	public void populateComments(JsonArray json) {
		Log.i(TAG, "Kommentare ausgeben");
		// get comments and id from json
		
		commentsContainer.removeAllViews();
		
		for (int i = 0; i < json.asArray().size(); i++) {
			String tempComment = json.asArray().get(i).asObject().get("context").asString();
			String tempName = json.asArray().get(i).asObject().get("name").asString();
			final String tempUserID = json.asArray().get(i).asObject().get("userID").asString();
			
			// create views
			TextView tv = new TextView(this);
			tv.setText(tempName);
			
			// ausgewählten User anzeigen
			tv.setOnClickListener(new OnClickListener() {
								
				@Override
				public void onClick(View v) {
					Intent i = new Intent(getBaseContext(), ShowUserProfileActivity.class);
					i.putExtra("id", Integer.parseInt(tempUserID));
					startActivity(i);
				}
			});
						
			commentsContainer.addView(tv);
			
			TextView tv2 = new TextView(this);
			tv2.setText(" " + tempComment);
			
			commentsContainer.addView(tv2);
		}

	}

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == SetInterestActivity.REQUEST_INTEREST) {
	        if (resultCode == RESULT_OK) {
	        	if(data != null){
	        		Log.d(TAG, data.getStringExtra("interests"));
	        	}
	        }
	    }
	}
	
	
}
